package com.mmall.controller.backend;

import com.mmall.common.ServerResponse;
import com.mmall.service.ICategoryService;
import com.mmall.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping("/manage/category")
public class CategoryController {

    @Autowired
    private IUserService iUserService;

    @Autowired
    private ICategoryService iCategoryService;

    /**
     * 添加品类
     * @param request
     * @param categoryName
     * @param parentId
     * @return
     */
    @RequestMapping(value = "addCategory")
    @ResponseBody
    public ServerResponse addCategory(HttpServletRequest request, String categoryName,
                                      @RequestParam(value = "parentId", defaultValue = "0") int parentId) {
//        String loginToken = CookieUtil.readLoginToken(request);
//        if (StringUtils.isEmpty(loginToken)) {
//            return ServerResponse.retErrorMsg("用户未登录，无法获取信息");
//        }
//        String userJsonStr = RedisPoolUtil.get(loginToken);
//        User user = JsonUtil.stringToObj(userJsonStr, User.class);
//        if (user == null) {
//            return ServerResponse.retErrorCode(ResponseCode.NEED_LOGIN.getCode(), "用户未登录,请登录");
//        }
//        if (iUserService.checkAdminRole(user).isSuccess()) {
//            //更新categoryName
//            return iProductService.saveOrUpdateProduct(product);
//        } else {
//            return ServerResponse.retErrorMsg("无权限操作,需要管理员权限");
//        }
        //全部通过拦截器验证是否登陆以及权限
        return iCategoryService.addCategory(categoryName, parentId);

    }

    /**重命名
     * @param request
     * @param categoryId
     * @param categoryName
     * @return
     */
    @RequestMapping("setCategoryName")
    @ResponseBody
    public ServerResponse setCategoryName(HttpServletRequest request,Integer categoryId,String categoryName){
        //全部通过拦截器验证是否登陆以及权限
        return iCategoryService.updateCategoryName(categoryId,categoryName);
    }

    /**查询子节点
     * @param request
     * @param categoryId
     * @return
     */
    @RequestMapping("getCategory")
    @ResponseBody
    public ServerResponse getChildrenParallelCategory(HttpServletRequest request,
                                                      @RequestParam(value = "categoryId" ,defaultValue = "0") Integer categoryId){
        return iCategoryService.getChildrenParallelCategory(categoryId);
    }

    /**查询所有子节点的id
     * @param request
     * @param categoryId
     * @return
     */
    @RequestMapping("getDeepCategory")
    @ResponseBody
    public ServerResponse getDeepChildrenCategory(HttpServletRequest request,@RequestParam(value = "categoryId" ,defaultValue = "0") Integer categoryId){
        //全部通过拦截器验证是否登陆以及权限
        return iCategoryService.selectCategoryAndChildrenById(categoryId);
    }
}
